<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select
          v-model="query.productCategory"
          clearable
          size="small"
          placeholder="商品类别"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in productCategoryOptions"
            :key="item.categoryId"
            :label="item.category"
            :value="item.category"
          />
        </el-select>
        <div class="rmb-distinction filter-item">
          <el-input
            v-model="query.lowPrice"
            placeholder="最低价格"
            style="width: 120px"
            @input="crud.toQuery"
          />
          <span class="mlr10">-</span>
          <el-input
            v-model="query.tallPrice"
            placeholder="最高价格"
            style="width: 120px"
            @input="crud.toQuery"
          />
        </div>
        <el-select
          v-model="query.productHotType"
          clearable
          size="small"
          placeholder="热度类型"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in productHotTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <el-select
          v-model="query.productHotUnit"
          clearable
          size="small"
          placeholder="热度单位"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in productHotUnitTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>

        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="ID">
            <el-input v-model="form.hotId" style="width: 370px" />
          </el-form-item>
          <el-form-item label="国家" prop="country">
            <el-sr v-model="form.productLink" style="width: 370px" />
          </el-form-item>
          <el-form-item label="商品图片">
            <el-input v-model="form.productImg" style="width: 370px" />
          </el-form-item>
          <el-form-item label="商品类别">
            <el-input v-model="form.productCategory" style="width: 370px" />
          </el-form-item>
          <el-form-item label="RMB价格">
            <el-input v-model="form.productRmbPrice" style="width: 370px" />
          </el-form-item>
          <!-- <el-form-item label='热度描述'>
            <el-input v-model='form.productHotDesc' style='width: 370px' />
          </el-form-item> -->
          <el-form-item label="热度类型">
            <el-input v-model="form.productHotType" style="width: 370px" />
          </el-form-item>
          <el-form-item label="热度单位">
            <el-input v-model="form.productHotUnit" style="width: 370px" />
          </el-form-item>
          <el-form-item label="热度数量">
            <el-input v-model="form.productHotNum" style="width: 370px" />
          </el-form-item>
          <el-form-item label="已售出">
            <el-input v-model="form.productSold" style="width: 370px" />
          </el-form-item>
          <el-form-item label="上架时间">
            <el-input v-model="form.day" style="width: 370px" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
        @sort-change="crud.sortChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="hotId" label="ID" />
        <el-table-column prop="country" label="国家" />

        <el-table-column prop="dept" label="主图">
          <template slot-scope="scope">
            <div>
              <el-tooltip placement="top" effect="light">
                <div slot="content">
                  <img class="tooltip-img" :src="scope.row.realImg">
                </div>
                <img

                  class="dept-img"
                  :src="scope.row.realImg"
                  @click="toLink(scope.row.productLink)"
                >
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="productCategory" label="商品类别" />
        <el-table-column prop="productRmbPrice" label="RMB价格" />
        <!-- <el-table-column prop='productHotDesc' label='热度描述' /> -->
        <el-table-column prop="productHotType" label="热度类型" />
        <el-table-column prop="productHotUnit" label="热度单位" />
        <el-table-column prop="productHotNum" label="热度数量" sortable="custom" />
        <el-table-column prop="productSold" label="已售出" sortable="custom" />
        <el-table-column prop="day" label="上架时间" sortable="custom">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.day) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin', 'goodsHot:edit', 'goodsHot:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGoodsHot from '@/api/product/goodsHot'
import { getAllCategory } from '@/api/data'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = {
  hotId: null,
  productLink: null,
  productImg: null,
  productId: null,
  productCategory: null,
  productPriceUnit: null,
  productPrice: null,
  productRmbPrice: null,
  productHotDesc: null,
  productHotType: null,
  lowPrice: null,
  tallPrice: null,
  productHotUnit: null,
  productHotNum: null,
  day: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'GoodsHot',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: 'product-hot',
      url: 'api/goodsHot',
      idField: 'hotId',
      sort: 'hotId,desc',
      isLoadImg: true,
      crudMethod: { ...crudGoodsHot }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'goodsHot:add'],
        edit: ['admin', 'goodsHot:edit'],
        del: ['admin', 'goodsHot:del']
      },
      productHotTypeOptions: [
        { key: '销量', display_name: '销量' },
        { key: '浏览', display_name: '浏览' }
      ],
      productHotUnitTypeOptions: [
        { key: '天', display_name: '天' },
        { key: '小时', display_name: '小时' }
      ],
      productCategoryOptions: [],
      rules: {
        productLink: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'productHotType', display_name: 'productHotType' },
        { key: 'productHotUnit', display_name: 'productHotUnit' },
        { key: 'productCategory', display_name: 'productCategory' },
        { key: 'lowPrice', display_name: 'lowPrice' },
        { key: 'tallPrice', display_name: 'tallPrice' }
      ]
    }
  },
  created() {
    this.getAllCategoryData()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    toLink(url) {
      window.open(url)
    },
    async getAllCategoryData() {
      this.productCategoryOptions = await getAllCategory()
    }
  }
}
</script>

<style lang='scss' scoped>
.dept-img {
  width: 80px;
  cursor: pointer;
  border: none;
}
.tooltip-img {
  width: 240px;
}
.rmb-distinction {
  display: inline-flex;
  align-items: center;
  span.mlr10 {
    margin: 0 3px;
  }
}
</style>
